/**
 * Copyright &copy; 2017-2020 <a href="http://www.fosung.com/">FosungFrame</a> All rights reserved.
 */
package com.fosung.frame.tsdemo.web.notice;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.fosung.frame.common.config.Global;
import com.fosung.frame.common.persistence.Page;
import com.fosung.frame.common.utils.StringUtils;
import com.fosung.frame.common.web.BaseController;
import com.fosung.frame.modules.cms.entity.Category;
import com.fosung.frame.modules.cms.service.CategoryService;
import com.fosung.frame.tsdemo.entity.notice.Notice;
import com.fosung.frame.tsdemo.service.notice.NoticeService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 通知信息Controller
 * @author liym
 * @version 2017-07-05
 */
@Controller
@RequestMapping(value = "${adminPath}/tsdemo/notice/notice")
public class NoticeController extends BaseController {

	@Autowired
	private NoticeService noticeService;
	@Autowired
	private CategoryService categoryService;
	
	@ModelAttribute
	public Notice get(@RequestParam(required=false) String id) {
		Notice entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = noticeService.get(id);
		}
		if (entity == null){
			entity = new Notice();
		}
		return entity;
	}
	
	@RequiresPermissions("tsdemo:notice:notice:view")
	@RequestMapping(value = {"list", ""})
	public String list(Notice notice, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Notice> page = noticeService.findPage(new Page<Notice>(request, response), notice); 
		model.addAttribute("page", page);
		return "frame/tsdemo/notice/noticeList";
	}

	@RequiresPermissions("tsdemo:notice:notice:view")
	@RequestMapping(value = "form")
	public String form(Notice notice, Model model) {
		model.addAttribute("notice", notice);
		return "frame/tsdemo/notice/noticeForm";
	}

	@RequiresPermissions("tsdemo:notice:notice:edit")
	@RequestMapping(value = "save")
	public String save(Notice notice, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, notice)){
			return form(notice, model);
		}
		if (notice.getImage()!=null){
			notice.setImage(StringEscapeUtils.unescapeHtml4(
					notice.getImage()));
		}
		if (notice.getContent()!=null){
			notice.setContent(StringEscapeUtils.unescapeHtml4(
					notice.getContent()));
		}
		noticeService.save(notice);
		addMessage(redirectAttributes, "保存通知信息成功");
		return "redirect:"+Global.getAdminPath()+"/tsdemo/notice/notice/?repage";
	}
	
	@RequiresPermissions("tsdemo:notice:notice:edit")
	@RequestMapping(value = "delete")
	public String delete(Notice notice, RedirectAttributes redirectAttributes) {
		noticeService.delete(notice);
		addMessage(redirectAttributes, "删除通知信息成功");
		return "redirect:"+Global.getAdminPath()+"/tsdemo/notice/notice/?repage";
	}
	
	@RequiresUser
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(String module, @RequestParam(required=false) String extId, HttpServletResponse response) {
		response.setContentType("application/json; charset=UTF-8");
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<Category> list = categoryService.findByUser(true, module);
		for (int i=0; i<list.size(); i++){
			Category e = list.get(i);
			if (extId == null || (extId!=null && !extId.equals(e.getId()) && e.getParentIds().indexOf(","+extId+",")==-1)){
				Map<String, Object> map = Maps.newHashMap();
				if (!"党建热点".equals(e.getName())) {
					map.put("id", e.getId());
					map.put("pId", e.getParent()!=null?e.getParent().getId():0);
					map.put("name", e.getName());
					map.put("module", e.getModule());
					mapList.add(map);
				}
			}
		}
		return mapList;
	}

}